//
//  HomeCell.swift
//  WeiBo
//
//  Created by LTX on 2016/12/1.
//  Copyright © 2016年 LTX. All rights reserved.
//

import UIKit
import SDWebImage

private let edgeMargin : CGFloat = 8
private let itemMargin : CGFloat = 10

class HomeCell: UITableViewCell {

    // MARK:- 控件属性
    @IBOutlet weak var IconView: UIImageView!
    
    @IBOutlet weak var verifiedType: UIImageView!
    
    @IBOutlet weak var userName: UILabel!
    
    @IBOutlet weak var vipImage: UIImageView!
    
    @IBOutlet weak var timeLable: UILabel!
    
    @IBOutlet weak var source: UILabel!
    
    @IBOutlet weak var content: UILabel!
    
    // MARK:- 约束
    @IBOutlet weak var picViewWCons: NSLayoutConstraint!
    @IBOutlet weak var picViewHCons: NSLayoutConstraint!
    
    @IBOutlet weak var picView: picCollectionView!
    
    
    // MARK:-  自定义属性
    var viewModel : StatusViewModel? {
        didSet {
        
            // nil值校验
            guard let viewModel = viewModel else {
                return
            }

            // 头像
            IconView.sd_setImage(with: viewModel.profileURL as URL!, placeholderImage: UIImage(named: "avatar_default_small"))
            
            //认证类型
            verifiedType.image = viewModel.verifiedImage
            //昵称
            userName.text = viewModel.status?.user?.screen_name
            //昵称颜色
            userName.textColor = viewModel.vipImage == nil ? UIColor.black : UIColor.orange
            //时间
            timeLable.text = viewModel.createAtText
            //来源
            source.text = viewModel.sourceText
            //会员等级
            vipImage.image = viewModel.vipImage
            //内容
            content.text = viewModel.status?.text
            
            //设置picView的高度和宽度
            let picViewSize = calculatePicViewSize(count: viewModel.picURLArray.count)
            picViewHCons.constant = picViewSize.height
            picViewWCons.constant = picViewSize.width
            
            picView.picURLArray = viewModel.picURLArray
            
        }
        
    }
    
    
    
    // MARK:- 系统回调函数
    override func awakeFromNib() {
        super.awakeFromNib()

//        HomeContentCellW.constant = UIScreen.main.bounds.height - 30
        
//        // 取出picView对应的layout
//        let layout = picView.collectionViewLayout as! UICollectionViewFlowLayout
//        
//        let imageHW = (UIScreen.main.bounds.width - 2 * edgeMargin - 2 * itemMargin) / 3
//        
//        layout.itemSize = CGSize(width: imageHW, height: imageHW)
    
    }
    
}


// MARK:- 计算方法
extension HomeCell{
    
   fileprivate func calculatePicViewSize(count : Int) -> CGSize {
        
        if count == 0 {
            return CGSize.zero
        }
    
    
    // 取出picView对应的layout
    let layout = picView.collectionViewLayout as! UICollectionViewFlowLayout
    
        //单张配图
    if count == 1 {
        // 取出图片
        let urlString = viewModel?.picURLArray.last?.absoluteString
        let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: urlString)
        
        layout.itemSize = CGSize(width: image!.size.width * 2, height: image!.size.height * 2)
        return CGSize(width: image!.size.width * 2, height: image!.size.height * 2)
    }
    
   
    
        //计算imageW H
        let imageViewWH = (UIScreen.main.bounds.width - 2 * edgeMargin - 2 * itemMargin)/3
        layout.itemSize = CGSize(width: imageViewWH, height: imageViewWH)
    
        //4张配图
        if count == 4 {
            let  imageW = 2 * imageViewWH + itemMargin
            return CGSize(width: imageW, height: imageW)
        }
    
        //其他张配图
        //计算行数
        let rows = CGFloat((count - 1) / 3 + 1)
        //计算imageView高度
        let imageH = rows * imageViewWH + (rows - 1)*itemMargin
    
        // 计算imageView宽度
        let imageW = UIScreen.main.bounds.width - 2 * edgeMargin
    
        return CGSize(width: imageW, height: imageH)
    
    }
    
}







